package com.example.esp8266;

import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.esp8266.databinding.EventItemBinding;

import java.util.ArrayList;
import java.util.List;

public class EventAdapter2 extends RecyclerView.Adapter<EventAdapter2.ViewHolder> {

    private final List<String> eventList = new ArrayList<>();

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(EventItemBinding.inflate(LayoutInflater.from(parent.getContext())));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        String item = eventList.get(position);
        holder.content.setText(item);
    }

    @Override
    public int getItemCount() {
        return eventList.size();
    }

    public void add(String event) {
        eventList.add(0, event);
        notifyItemInserted(0);
    }

    public void append(String event) {
        eventList.add(event);
        notifyItemChanged(eventList.size() - 1);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        private final LineNodeView lineNodeView;
        private final TextView content;

        public ViewHolder(@NonNull EventItemBinding binding) {
            super(binding.getRoot());
            lineNodeView = binding.line;
            content = binding.content;
        }
    }
}
